package models;

import java.io.Serializable;

import javax.persistence.*;

import org.apache.commons.lang3.StringUtils;

import prefix.util.CipherUtil;

import java.math.BigInteger;
import java.util.Date;

/**
 * The persistent class for the actor database table.
 * 
 */
@Entity
@Table(name = "ACTOR")
public class Actor implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @TableGenerator(
            name = "ACTOR_SEQ",
            table = "SEQUENCE",
            pkColumnName = "NAME",
            pkColumnValue = "ACTOR_SEQ",
            valueColumnName = "CNT",
            initialValue = 10000,
            allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "ACTOR_SEQ")
    @Column(unique = true, nullable = false)
    private Integer id;

    @Column(name = "A_STATUS")
    private int aStatus;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "A_STATUS_D")
    private Date aStatusD;

    @Column(name = "B_STATUS")
    private int bStatus;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "B_STATUS_D")
    private Date bStatusD;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATE_D")
    private Date createD;

    @Column(length = 100)
    private String credential;

    @Temporal(TemporalType.TIMESTAMP)
    private Date dob;

    @Column(length = 100)
    private String email;

    @Column(name = "FAILED_AUTH_CNT")
    private int failedAuthCnt;

    private BigInteger flag;

    @Column(name = "FULL_NAME", length = 50)
    private String fullName;

    private int honourific;

    @Column(name = "IDENTITY_NUM", length = 100)
    private String identityNum;

    @Column(name = "IDENTITY_TYPE")
    private int identityType;

    @Column(name = "L_STATUS")
    private int lStatus;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "L_STATUS_D")
    private Date lStatusD;

    private int marital;

    @Column(name = "MON_INCOME")
    private double monIncome;

    private int subtype;

    @Column(name = "T_STATUS")
    private int tStatus;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "T_STATUS_D")
    private Date tStatusD;

    private int tenure;

    private int type;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "UPDATE_D")
    private Date updateD;

    @Column(length = 100)
    private String username;

    @Column(name = "HOUSE_STATUS")
    private int houseStatus;

    @Column(name = "EDUCATION_LEVEL")
    private int educationLevel;

    private String mobile;

    public Actor() {
    }

    public void setPesonalInfo(String userName, String idNum, Actor.MaritalStatus maritalStatus,
            Actor.Honourific gender, Actor.IdentityType idType, Date dob, Actor.HousingStatus houseStatus,
            Actor.EducationLevel educationLevel, double mthIncome, String name) {
        if (StringUtils.isNotEmpty(userName))
            this.setUsername(userName);
        if (StringUtils.isNotEmpty(idNum))
            this.setIdentityNum(idNum);
        if (idType != null)
            this.setIdentityType(idType);
        if (maritalStatus != null)
            this.setMarital(maritalStatus);
        if (gender != null)
            this.setHonourific(gender);
        if (dob != null)
            this.setDob(dob);
        if (houseStatus != null)
            this.setHouseStatus(houseStatus);
        if (educationLevel != null)
            this.setEducationLevel(educationLevel);
        if (mthIncome > 0)
            this.setMonIncome(mthIncome);
        if(StringUtils.isNotEmpty(name))
            this.setFullName(name);
    }

    public void setLoginInfo() {

    }

    public static enum Status {
        ANY, // 0
        UNCONFIRM, // 1
        CONFIRM, // 2

    }

    public static enum Honourific {
        MALE, //
        FEMALE, //
    }

    public static enum IdentityType {
        ID_CARD, // 0
        PASSPORT, // 1
    }

    private static Status[] confirmBorrower = { Status.CONFIRM };

    public boolean isConfirmBorrower() {
        for (Status item : confirmBorrower) {
            if (this.bStatus == item.ordinal()) {
                return true;
            }
        }
        return false;
    }

    private static Status[] confirmLender = { Status.CONFIRM };

    public boolean isConfirmLender() {
        for (Status item : confirmLender) {
            if (this.lStatus == item.ordinal()) {
                return true;
            }
        }
        return false;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getAStatus() {
        return this.aStatus;
    }

    public void setAStatus(int aStatus) {
        this.aStatus = aStatus;
    }

    public Date getAStatusD() {
        return this.aStatusD;
    }

    public void setAStatusD(Date aStatusD) {
        this.aStatusD = aStatusD;
    }

    public int getBStatus() {
        return this.bStatus;
    }

    public void setBStatus(int bStatus) {
        this.bStatus = bStatus;
    }

    public Date getBStatusD() {
        return this.bStatusD;
    }

    public void setBStatusD(Date bStatusD) {
        this.bStatusD = bStatusD;
    }

    public Date getCreateD() {
        return this.createD;
    }

    public void setCreateD(Date createD) {
        this.createD = createD;
    }

    public String getCredential() {
        return this.credential;
    }

    public void setCredential(String credential) {
    	if(credential!=null){
    		this.credential=CipherUtil.generatePassword(credential);
    	}
        this.credential = credential;
    }

    public Date getDob() {
        return this.dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getFailedAuthCnt() {
        return this.failedAuthCnt;
    }

    public void setFailedAuthCnt(int failedAuthCnt) {
        this.failedAuthCnt = failedAuthCnt;
    }

    public BigInteger getFlag() {
        return this.flag;
    }

    public void setFlag(BigInteger flag) {
        this.flag = flag;
    }

    public String getFullName() {
        return this.fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Honourific getHonourific() {
        return Honourific.values()[this.honourific];
    }

    public void setHonourific(Honourific honourific) {
        this.honourific = honourific.ordinal();
    }

    public String getIdentityNum() {
        return this.identityNum;
    }

    public void setIdentityNum(String identityNum) {
        this.identityNum = identityNum;
    }

    public IdentityType getIdentityType() {
        return IdentityType.values()[this.identityType];
    }

    public void setIdentityType(IdentityType identityType) {
        this.identityType = identityType.ordinal();
    }

    public int getLStatus() {
        return this.lStatus;
    }

    public void setLStatus(int lStatus) {
        this.lStatus = lStatus;
    }

    public Date getLStatusD() {
        return this.lStatusD;
    }

    public void setLStatusD(Date lStatusD) {
        this.lStatusD = lStatusD;
    }

    public MaritalStatus getMarital() {
        return MaritalStatus.values()[this.marital];
    }

    public void setMarital(MaritalStatus marital) {
        this.marital = marital.ordinal();
    }

    public double getMonIncome() {
        return this.monIncome;
    }

    public void setMonIncome(double monIncome) {
        this.monIncome = monIncome;
    }

    public int getSubtype() {
        return this.subtype;
    }

    public void setSubtype(int subtype) {
        this.subtype = subtype;
    }

    public int getTStatus() {
        return this.tStatus;
    }

    public void setTStatus(int tStatus) {
        this.tStatus = tStatus;
    }

    public Date getTStatusD() {
        return this.tStatusD;
    }

    public void setTStatusD(Date tStatusD) {
        this.tStatusD = tStatusD;
    }

    public int getTenure() {
        return this.tenure;
    }

    public void setTenure(int tenure) {
        this.tenure = tenure;
    }

    public TYPE getType() {
        return TYPE.values()[this.type];
    }

    public void setType(TYPE type) {
        this.type = type.ordinal();
    }

    public Date getUpdateD() {
        return this.updateD;
    }

    public void setUpdateD(Date updateD) {
        this.updateD = updateD;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public static enum MaritalStatus {
        ANY("Internal"), // 0
        SINGLE("Single"), // 1
        MARRIED("Married"), // 2
        DIVORCED("Divorced"), // 3
        ;
        private String _label;

        MaritalStatus(String label) {
            this._label = label;
        }

        public String getLabel() {
            return this._label;
        }

        public static MaritalStatus getMaritalStatusByLabel(String enumString) {
            for (MaritalStatus maritalStatus : MaritalStatus.values()) {
                if (enumString != null && enumString.equals(maritalStatus.getLabel())) {
                    return maritalStatus;
                }
            }
            return MaritalStatus.ANY;
        }
    }

    public enum TYPE {
        ANY,
        SYSTEM,
        CUSTOMER,
        OPERATOR;
    }

    public HousingStatus getHouseStatus() {
        return HousingStatus.values()[this.houseStatus];
    }

    public void setHouseStatus(HousingStatus houseStatus) {
        this.houseStatus = houseStatus.ordinal();
    }

    public EducationLevel getEducationLevel() {
        return EducationLevel.values()[this.educationLevel];
    }

    public void setEducationLevel(EducationLevel educationLevel) {
        this.educationLevel = educationLevel.ordinal();
    }

    public static enum HousingStatus {
        ANY, // 0 -- internal for use by admin app only
        NONE, // 1 --
        OWN, // 2 --
        RENT, // 3 -- internal for use by admin app only
        MORTGAGE, // 4 --
        OTHER // 5 --
        ;

        public static HousingStatus getStatus(String str) {
            if (str == null) {
                return ANY;
            } else if (str.equalsIgnoreCase("own")) {
                return OWN;
            } else if (str.equalsIgnoreCase("rent")) {
                return RENT;
            } else if (str.equalsIgnoreCase("mortgage")) {
                return MORTGAGE;
            } else if (str.equalsIgnoreCase("other")) {
                return OTHER;
            }
            return ANY;
        }
    }

    public static enum EducationLevel {
        ANY("any"), // 0 -- internal for use by admin app only
        PHD_AND_ABOVE("Ph.D. & above"),
        MASTERS("Master"),
        BACHELOR("Bachelor"),
        UNGRADUATE("Undergraduate"),
        HIGH_SCHOOL("High School"),
        SECONDARY_OR_VOCATIONAL("Secondary or Vocational"),
        MIDDLE_SCHOOL_OR_BELLOW("Middle School or bellow");
        private String _label;

        EducationLevel(String label) {
            this._label = label;
        }

        public String getLabel() {
            return this._label;
        }
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

}
